<!DOCTYPE html>
<html lang="en">
<head>
    <!--
    @Author 硕鼠
    @Date 2020/4/17 8:26
    @Version 1.0
    @Description 定时器的使用
  -->
    <meta charset="UTF-8">
    <title>Title</title>

    <script>
        window.onload = function () {
            var oBut1 = document.getElementById('btn1');
            var oBut2 = document.getElementById('btn2');
            var timer = null;

            oBut1.onclick= function () {
                timer = setInterval(function (){
                            alert("a");
                },3000); //间隔1S
            };

            oBut2.onclick= function () {
                clearInterval(timer); //关闭定时器
            };
        };
    </script>

</head>
<body>

<input id="btn1" type="button" value="开启"/>
<input id="btn2" type="button" value="关闭"/>



</body>
</html>